﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bussiness;

namespace WebApplication
{
    public class Commomcs
    {
        public static DateTime convertDate(string date)
        {
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);

            return DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
        }
        public static string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
        /// <summary>
        /// Hàm duyệt ngày tháng
        /// </summary>
        /// <returns></returns>
        public static DateTime DuyenNgayThang( string mamh)
        {
            List<TON_KHO> LstTonKho = dataprovider.Entity.TON_KHO.Where(p => p.MAMH == mamh).ToList();
            DateTime dt = new DateTime();
            if (LstTonKho.Count > 0)
            {
                if (LstTonKho.Count == 1)
                {
                    dt = LstTonKho[0].NGAYTHANG;

                }
                if (LstTonKho.Count > 1)
                {
                    foreach (TON_KHO Tonkho in LstTonKho)
                    {
                        foreach (TON_KHO Tonkho1 in LstTonKho)
                        {
                            if (Tonkho.NGAYTHANG > Tonkho1.NGAYTHANG)
                            {
                                dt = Tonkho.NGAYTHANG;
                            }
                        }

                    }
                }

            }
            dt = convertDate(dt.ToString("dd/MM/yyyy"));
            return dt;

        }
        public static DateTime DuyenNgayThangtonvo(string mamh)
        {
            List<TONKHO_VO> LstTonKho = dataprovider.Entity.TONKHO_VO.Where(p => p.MAMH == mamh).ToList();
            DateTime dt = new DateTime();
            if (LstTonKho.Count > 0)
            {
                if (LstTonKho.Count == 1)
                {
                    dt = LstTonKho[0].NGAYTHANG;

                }
                if (LstTonKho.Count > 1)
                {
                    foreach (TONKHO_VO Tonkho in LstTonKho)
                    {
                        foreach (TONKHO_VO Tonkho1 in LstTonKho)
                        {
                            if (Tonkho.NGAYTHANG > Tonkho1.NGAYTHANG)
                            {
                                dt = Tonkho.NGAYTHANG;
                            }
                        }

                    }
                }
            }
            dt = convertDate(dt.ToString("dd/MM/yyyy"));
            return dt;

        }
        public static CHUCNANG QuyenTruyCapChucnang(string strFormName, string MaNV)
        {
            string strName = "";
            CHUCNANG chucnang = null;
            if (!string.IsNullOrEmpty(strFormName))
            {
                int idx = strFormName.LastIndexOf("/");
                strName = strFormName.Substring(idx + 1, strFormName.Length - idx - 1).ToLower();
                IUnitOfWork iunitfwork = new EFUnitOfWork();
                CHUCNANGRepository repositoryChucnang = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
                NHANVIENRepository repositoryNhanvien = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
                QUYENRepository repositoryquyen=new QUYENRepository(new EFRepository<QUYEN>(),iunitfwork);
                NGUOUDUNG_CHUCNANGRepository repositoryNguoiDungCn=new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(),iunitfwork);
                List<int> LstIDchucnnang = repositoryNguoiDungCn.All().Where(p => p.MANV == MaNV).Select(t =>(int) t.ID_CHUCNANG).ToList();
                List<int> LstIdQuyen=repositoryChucnang.All().Where(p=>LstIDchucnnang.Contains(p.ID)).Select(t=>(int)t.ID_QUYEN).ToList();
               IQueryable<CHUCNANG> objchucnang=repositoryChucnang.All();
               //foreach(int i in LstIDchucnnang)
               //{
               //    foreach(CHUCNANG cn in objchucnang)
               //    {
               //        if(cn.ID==i)
               //        {
               //            LstIdQuyen.Add((int)cn.ID_QUYEN);
               //        }
               //    }
               //}
                QUYEN objquyen = repositoryquyen.All().Where(p => p.FROMNAME.ToLower().Equals(strName)).FirstOrDefault();
                if (LstIdQuyen.Count > 0 && objquyen !=null)
                {
                    List<CHUCNANG> Lstchucnang =repositoryChucnang.All().Where(p => LstIdQuyen.Contains((int)p.ID_QUYEN) && LstIDchucnnang.Contains(p.ID) && p.ID_QUYEN==objquyen.ID).ToList();
                    if (Lstchucnang.Count > 0 && objquyen != null)
                    {
                        chucnang = Lstchucnang[0];
                    }
                }
            }
            return chucnang;
        }

    }
}